<?php
class apiactionlog_ctl_admin_erplog extends desktop_controller{

    var $workground = "apiactionlog.wrokground.erplog";
    function index($status='all'){
        $base_filter = '';
        $orderby = ' calltime desc ';
        switch($status){
            case 'all':
                $this->title = '所有同步日志';
                break;
            case 'success':
                $this->title = '同步成功日志';
                $base_filter = array('status'=>'success');
                break;
            case 'fail':
                $this->title = '同步失败日志';
                $base_filter = array('status'=>'fail');
                break;
        }

        if ($status=='fail' ){
            $actions =
                array(
                    array(
                        'label' => '批量重试',
                        'submit' => 'index.php?app=apiactionlog&ctl=admin_erplog&act=re_request',
                       //dialog::{width:550,height:300,title:'批量重试'}",
                    ),
                );
        }

        $params = array(
            'title'=>$this->title,
            'actions'=> $actions,
            'use_buildin_new_dialog' => false,
            'use_buildin_set_tag'=>false,
            'use_buildin_recycle'=>false,
            'use_buildin_export'=>false,
            'use_buildin_import'=>false,
            'use_buildin_filter'=>true,
            'orderBy' => $orderby,
        );

        if($base_filter){
            $params['base_filter'] = $base_filter;
        }

        $this->finder('apiactionlog_mdl_erplog',$params);
    }

    function retry($log_id='', $retry_type='single'){
        if ($retry_type == 'single'){
            $this->pagedata['log_id'] = $log_id;
        }else{
            if (is_array($log_id['log_id'])){
                $this->pagedata['log_id'] = implode("|", $log_id['log_id']);
            }
        }
        $this->pagedata['isSelectedAll'] = $log_id['isSelectedAll'];
        $this->pagedata['retry_type'] = $retry_type;
        $this->display("admin/api/retry.html");
    }

    function re_request(){
        $this->begin();
        $write_log=kernel::single('b2c_ctl_admin_goods');
        $erplogObj=$this->app->model('erplog');
        $erp_caller_obj = kernel::service('apierp.rpc_erp_request');
        if($_POST['erplog_id']){
            foreach ($_POST['erplog_id'] as $key => $erplog_id) {
                $api_mess[$erplog_id] = $this->app->model('erplog')->getRow('method,params',array('erplog_id'=>$erplog_id));      
            }
            foreach ($api_mess as $erplog_id => $post) {                   
                    $rep_message = $erp_caller_obj->call_api_yucai($post);
                    $retry=$this->app->model('erplog')->getRow('retry',array('erplog_id'=>$erplog_id));
                    $retry=$retry['retry']+1;
                    if($rep_message['GetBasicDataResult']=='false'){
                        $last_modified=time();
                        $erplogObj->update(array('last_modified'=>$last_modified,'msg'=>$rep_message['message'],'retry'=>$retry),array('erplog_id'=>$erplog_id));
                    }else{
                        $last_modified=time();
                        $erplogObj->update(array('last_modified'=>$last_modified,'status'=>'success','msg'=>$rep_message['message'],'retry'=>$retry),array('erplog_id'=>$erplog_id));
                    }

            }
            $this->end('操作完成');
        }elseif($_GET['erplog_id']){
            $erplog_id = $_GET['erplog_id'];
            $api_mess = $this->app->model('erplog')->getRow('method,params',array('erplog_id'=>$erplog_id));

            $rep_message = $erp_caller_obj->call_api_yucai($api_mess);

            $retry=$this->app->model('erplog')->getRow('retry',array('erplog_id'=>$erplog_id));
            $retry=$retry['retry']+1;
            if($rep_message['GetBasicDataResult']=='false'){
                $last_modified=time();
                $erplogObj->update(array('last_modified'=>$last_modified,'msg'=>$rep_message['message'],'retry'=>$retry),array('erplog_id'=>$erplog_id));
            }else{
                $last_modified=time();
                $erplogObj->update(array('last_modified'=>$last_modified,'status'=>'success','msg'=>$rep_message['message'],'retry'=>$retry),array('erplog_id'=>$erplog_id));
            }
            $this->end('操作完成');
        }
    }

    function batch_retry(){
        $this->retry($_POST, 'batch');
    }

}
